<?php
/**
 * Geneone: Content Management Framework
 * Templating Class
 *
 * http://geneone.neondragon.net/
 *
 * @package Geneone
 * @author Khlo
 * @version 1.0
 * @copyright 2004-2006 Geneone Development Team
 */
 
class Gene_Template {
	var $templateDir;
	var $templateVars = array();
	var $debugTemplate = false;
	
	/**
	 * Assigns a value to a template variable.
	 *
	 * @var string $name Variable Name
	 * @var string $value Variable Value
	 */
	function assign($name, $value) {
		if (strtolower($name) != 'this') {
			$this->templateVars[$name] = $value;
		}
	}
	
	/**
	 * Assigns a value to a template variable by reference.
	 *
	 * @var string $name Variable Name
	 * @var string $value Variable Value
	 */
	function assignRef($name, &$value) {
		if (strtolower($name) != 'this') {
			$this->templateVars[$name] =& $value;
		}
	}
	
	/**
	 * Appends a value to a template array variable.
	 *
	 * @var string $name Variable Name
	 * @var string $value Variable Value
	 */
	function append($name, $value) {
		if (strtolower($name) != 'this') {
			if (isset($this->templateVars[$name]) && is_array($this->templateVars[$name])) {
				array_push($this->templateVars[$name], $value);
			} else {
				$this->templateVars[$name][] = $value;
			}
		}
	}
	
	/**
	 * Appends a value to a template array variable by reference.
	 *
	 * @var string $name Variable Name
	 * @var string $value Variable Value
	 */
	function appendRef($name, $value) {
		if (strtolower($name) != 'this') {
			if (isset($this->templateVars[$name]) && is_array($this->templateVars[$name])) {
				array_push($this->templateVars[$name], &$value);
			} else {
				$this->templateVars[$name][] =& $value;
			}
		}
	}
	
	/**
	 * Gets the value of a template variable.
	 *
	 * @var string $name Variable Name
	 *
	 * @return mixed Variable Value
	 */
	function get($name) {
		if (isset($this->templateVars[$name])) {
			return $this->templateVars[$name];
		}
	}
	
	/**
	 * Get an array of assigned template variables.
	 *
	 * @return array Assigned Variables
	 */
	function getAssigned() {
		return $this->templateVars;
	}
	
	/**
	 * Fetches a template and returns the contents.
	 *
	 * @var string $templateName Template Name
	 * @var string $variables Template Variables
	 * @var boolean $text Whether the template name is the template contents as text
	 *
	 * @return string Parsed Template Content
	 */
	function fetch($templateName, $variables=array(), $text=false) {
		$initial = error_reporting();
		
		if ($this->debugTemplate) {
			error_reporting(E_ALL);
		} else {
			error_reporting(E_ALL ^ E_NOTICE); // Shut up
		}
		
		ob_start(); // Use output buffer to capture template output
		
		// Extract into current scope
		extract($this->templateVars, EXTR_REFS);
		
		// Template specific variables
		if (count($variables)) {
			extract($variables, EXTR_OVERWRITE | EXTR_REFS);
		}
		
		if ($text) {
			eval('?>'.$templateName);
		} else {
			include ($this->templateDir.$templateName.".tpl.php");
		}
		
		$contents = ob_get_contents();
		ob_end_clean();
		
		error_reporting($initial);
		
		return $contents;
	}
	
	/**
	 * Parses a template and returns the contents.
	 *
	 * @var string $templateName Template Contents
	 * @var string $variables Template Variables
	 *
	 * @return string Parsed Template Content
	 */
	function parse($templateText, $variables=array()) {
		return $this->fetch($templateText, $variables, true);
	}
	
	/**
	 * Fetches a template and displays the contents.
	 *
	 * @var string $templateName Template Name
	 * @var string $variables Template Variables
	 */
	function display($templateName, $variables=array()) {
		echo $this->fetch($templateName, $variables);
	}
}
?>